package com.cskaoyan.wordmemorize.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cskaoyan.wordmemorize.common.aspect.operate.OperateLog;
import com.cskaoyan.wordmemorize.common.aspect.operate.OperateLogConstant;
import com.cskaoyan.wordmemorize.converter.LevelConverter;
import com.cskaoyan.wordmemorize.dao.entity.LevelPrivilegeDO;
import com.cskaoyan.wordmemorize.dao.mapper.LevelPrivilegeMapper;
import com.cskaoyan.wordmemorize.dto.admin.DeleteLevelPrivilegeOperateDTO;
import com.cskaoyan.wordmemorize.dto.admin.LevelPrivilegeDTO;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.request.BatchDeleteCommand;
import com.cskaoyan.wordmemorize.request.LevelPrivilegeCommand;
import com.cskaoyan.wordmemorize.request.LevelPrivilegePageRequest;
import com.cskaoyan.wordmemorize.service.LevelPrivilegeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author sgl
 * @version 1.0
 * @description: TODO
 * @Date 2025/07/16
 */
@Service
public class LevelPrivilegeServiceImpl implements LevelPrivilegeService {

    @Autowired
    LevelPrivilegeMapper levelPrivilegeMapper;

    @Autowired
    LevelConverter levelConverter;

    @Override
    public PageDTO<LevelPrivilegeDTO> getLevelPrivilegeList(LevelPrivilegePageRequest pageRequest) {

        // 获取参数
        Integer pageNum = pageRequest.getPageNum();
        Integer count = pageRequest.getCount();
        String levelId = pageRequest.getLevelId();
        String keyword = pageRequest.getKeyword();

        // 构建分页查询的条件
        Page<LevelPrivilegeDO> pageParam = new Page<>(pageNum,count);

        if(levelId == null && keyword == null){
            Page<LevelPrivilegeDO> doPage = levelPrivilegeMapper.selectPage(pageParam, null);
            return levelConverter.levelPrivilegeDOToPageDTO(doPage.getRecords(), (int) doPage.getTotal());
        }
        LambdaQueryWrapper<LevelPrivilegeDO> levelPrivilegeDOWrapper = new LambdaQueryWrapper<>();
        levelPrivilegeDOWrapper.eq(levelId != null,LevelPrivilegeDO::getLevelValue,levelId)
                .like(keyword!= null,LevelPrivilegeDO::getPrivilegeCode,keyword);

        Page<LevelPrivilegeDO> levelPrivilegeDOs =
                levelPrivilegeMapper.selectPage(pageParam, levelPrivilegeDOWrapper);

        return levelConverter.levelPrivilegeDOToPageDTO(levelPrivilegeDOs.getRecords(), (int) levelPrivilegeDOs.getTotal());
    }

    /**
     * 添加等级特权
     * @param command
     */
    @Override
    @OperateLog(dateType = "角色权限",operateType = OperateLogConstant.OPERATE_ADD)
    public void addLevelPrivilege(LevelPrivilegeCommand command) {
        LevelPrivilegeDO levelPrivilegeDO =
                levelConverter.levelPrivilegeCommandToLevelPrivilegeDO(command);

        levelPrivilegeMapper.insert(levelPrivilegeDO);
    }

    @Override
    @OperateLog(dateType = "角色权限",operateType = OperateLogConstant.OPERATE_UPDATE)
    public void updateLevelPrivilege(LevelPrivilegeCommand command) {
        LevelPrivilegeDO levelPrivilegeDO =
                levelConverter.levelPrivilegeCommandToLevelPrivilegeDO(command);

        LambdaUpdateWrapper<LevelPrivilegeDO> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(LevelPrivilegeDO::getId,levelPrivilegeDO.getId());
        levelPrivilegeMapper.update(levelPrivilegeDO,updateWrapper);

    }

    @Override
    @OperateLog(dateType = "角色权限",operateType = OperateLogConstant.OPERATE_SINGLE_DELETE)
    public DeleteLevelPrivilegeOperateDTO deleteLevelPrivilege(Long id) {
        LevelPrivilegeDO levelPrivilegeDO = levelPrivilegeMapper.selectById(id);
        levelPrivilegeMapper.deleteById(id);
        return levelConverter.levelPrivilegeDOToDeleteLevelPrivilegeOperateDTO(levelPrivilegeDO);
    }

    @Override
    @OperateLog(dateType = "角色权限",operateType = OperateLogConstant.OPERATE_BATCH_DELETE)
    public List<DeleteLevelPrivilegeOperateDTO> batchDeleteLevelPrivilege(BatchDeleteCommand command) {
        List<String> ids = command.getIds();
        List<LevelPrivilegeDO> levelPrivilegeDOS = levelPrivilegeMapper.selectByIds(ids);
        levelPrivilegeMapper.deleteByIds(ids);
        return levelConverter.levelPrivilegeDOsToDeleteLevelPrivilegeOperateDTOs(levelPrivilegeDOS);
    }
}
